package com.wang.store.service.impl;

import com.wang.store.entity.Address;
import com.wang.store.mapper.AddressMapper;
import com.wang.store.service.IAddressService;
import com.wang.store.service.IDistrictService;
import com.wang.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    IDistrictService iDistrictService;
    @Value("${user.address.max-count}")
    private int maxCount;


    @Override
    public void addNewAddress(Address address, Integer uid, String username) {
        Integer count = addressMapper.countByUid(uid);
       if (count>=maxCount){
           throw new AddressCountLimitException("用户收货地址大于20条");
       }
        String provinceCode = iDistrictService.getNameByCode(address.getProvinceCode());
        String cityCode = iDistrictService.getNameByCode(address.getCityCode());
        String areaCode = iDistrictService.getNameByCode(address.getAreaCode());

        address.setProvinceName(provinceCode);
        address.setCityName(cityCode);
        address.setAreaName(areaCode);

        address.setUid(uid);
       Integer isdefault = count==0? 1 : 0; //1代表默认, 0代表不默认
       address.setIsDefault(isdefault);
       //补全四项日志
       address.setModifiedUser(username);
       address.setCreatedUser(username);
       address.setModifiedTime(new Date());
       address.setCreatedTime(new Date());

        Integer integer = addressMapper.insertData(address);
        if (integer!=1){
            throw  new InsertException("插入时出现了未知异常");
        }


    }

    @Override
    public List<Address> getByuid(Integer uid) {
        List<Address> byUid = addressMapper.findByUid(uid);
        for (Address address :  byUid) {
            //address.setAid(null);
            //address.setUid(null);
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setTel(null);
            address.setCreatedTime(null);
            address.setCreatedUser(null);
            address.setModifiedTime(null);
            address.setModifiedUser(null);
        }
        return byUid;
    }

    @Override
    public void setDefault(Integer aid, Integer uid, String Username) {
        Address result = addressMapper.findByAidAndAddress(aid);
        if (result==null){
            throw  new AddressNotFoundException("用户地址没有找到");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }
        Integer rows = addressMapper.updateNonDefule(uid);
        if (rows < 1){
            throw  new UpdateException("更新数据时产生了未知异常");
        }

        Integer row = addressMapper.updateDefaultByAid(aid, Username, new Date());
        if (row!=1){
            throw  new UpdateException("更新数据产生了未知异常");
        }
    }

    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAidAndAddress(aid);
        if (result==null){
            throw new AddressNotFoundException("地址找不到");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }

        Integer rows = addressMapper.deleteByAid(aid);
        if (rows!=1){
            throw  new DeleteException("用户删除数据产生了未知异常");
        }
        Integer integer = addressMapper.countByUid(uid);
        if (integer==0){
            return;
        }

        if (result.getIsDefault()==0){
            return ;
             }
        Address lastModified = addressMapper.findLastModified(uid);
        rows = addressMapper.updateDefaultByAid(lastModified.getAid(), username, new Date());


        if (rows != 1) {
            throw new UpdateException("更新时产生了异常");
        }

    }


}
